package com.yutel.phone.binh.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.yutel.phone.binh.R;
import com.yutel.phone.binh.data.vo.Category;
import com.yutel.phone.binh.data.vo.Channel;
import com.yutel.phone.binh.service.HomeService;
import com.yutel.phone.binh.service.ServiceReceiver;
import com.yutel.phone.binh.service.SyncService;
import com.yutel.phone.binh.ui.adapter.ChannelAdapter;

public class HomeActivity extends BaseActivity implements OnItemClickListener,
		ServiceReceiver.Receiver {
	final static String TAG = "HomeActivity";
	private ServiceReceiver mReceiver;
	private ProgressBar pb;
	private ListView lv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.daybill_list);
		mReceiver = new ServiceReceiver(new Handler());
		mReceiver.setReceiver(this);
		pb = (ProgressBar) findViewById(android.R.id.empty);
		lv = (ListView) findViewById(android.R.id.list);
		Intent intent = new Intent(Intent.ACTION_SYNC, null, this,
				HomeService.class);
		intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, mReceiver);
		startService(intent);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		final Channel channel = (Channel) arg1.getTag();
		if (channel != null) {
			List<Category> list = channel.getCategoryList();
			if (list != null && list.size() > 0) {
				String[] category = new String[list.size()];
				for (int i = 0; i < list.size(); i++) {
					category[i] = list.get(i).getName();
				}
				OnClickListener click=new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Log.i(TAG, "which=" + which);
						Intent intent = new Intent(HomeActivity.this, CategoryActivity.class);
						intent.putExtra("channel", channel);
						intent.putExtra("categoryid", which);
						HomeActivity.this.startActivity(intent);
					}
					
				};
				new AlertDialog.Builder(this).setTitle(channel.getName())
						.setItems(category, click).setNegativeButton("取消", null)
						.show();
			}
		}

	}

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		Log.i(TAG, "onReceiveResult=" + resultCode);
		switch (resultCode) {
		case HomeService.STATUS_RUNNING:
			break;
		case HomeService.STATUS_FINISHED:
			ArrayList<Channel> mDataList = (ArrayList<Channel>) resultData
					.getSerializable(HomeService.class.getSimpleName());
			if (mDataList != null && mDataList.size() > 0) {
				Log.i(TAG, "mDataList=" + mDataList.size());
				lv.setAdapter(new ChannelAdapter(mDataList, this));
				lv.setOnItemClickListener(this);
			}
			break;
		case HomeService.STATUS_ERROR:
			Log.e(TAG, "warring resultCode=" + resultCode);
			pb.setVisibility(View.GONE);
			break;
		default:
			pb.setVisibility(View.GONE);
			Log.w(TAG, "warring resultCode=" + resultCode);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.control, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menui_stirring:
			Intent stirring = new Intent(this, StirringActivity.class);
			startActivity(stirring);
			break;
		case R.id.menui_search:
			Intent search = new Intent(this, SearchActivity.class);
			startActivity(search);
			break;
		case R.id.menui_config:
			Intent config = new Intent(this, ConfigActivity.class);
			startActivity(config);
			break;
		default:
			Log.e(TAG, "default=" + item.getItemId());
		}
		return true;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 是否退出应用
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.app_quit);
			builder.setIcon(android.R.drawable.ic_dialog_alert);
			builder.setPositiveButton(R.string.btn_confirm,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							finishActivitys();
						}
					});
			builder.setNegativeButton(R.string.btn_cancel,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			builder.show();
			return true;
		}
		return false;
	}
}